/*
 * Copyright (c) 2011 Shanda Corporation. All rights reserved.
 *
 * Created on 2011-11-23.
 */
package com.sd.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.telephony.TelephonyManager;

/**
 * String工具类
 * 
 * @author david
 */
public class StringUtils {

	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static boolean isNotEmpty(String str) {
		return !StringUtils.isEmpty(str);
	}

	public static boolean isBlank(CharSequence str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if ((Character.isWhitespace(str.charAt(i)) == false)) {
				return false;
			}
		}
		return true;
	}

	public static boolean isNotBlank(String str) {
		return !StringUtils.isBlank(str);
	}

	public static boolean isNumeric(CharSequence cs) {
		if (cs == null || cs.length() == 0) {
			return false;
		}
		int sz = cs.length();
		for (int i = 0; i < sz; i++) {
			if (Character.isDigit(cs.charAt(i)) == false) {
				return false;
			}
		}
		return true;
	}

	public static String getValueByKey(String result, String key) {
		String value = null;
		if (!isEmpty(result)) {
			try {
				JSONObject jsonObject = new JSONObject(result);
				String resultCode = jsonObject.getString("Result");
				if (resultCode != null && "0".equals(resultCode)) {
					value = jsonObject.getString(key);
				} else {
					System.out.println("result may be have some problem .");
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return value;
	}
	
	public static String getValueByKey0(String result, String key) {
		String value = null;
		if (!isEmpty(result)) {
			try {
				JSONObject jsonObject = new JSONObject(result);
			    value = jsonObject.getString(key);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return value;
	}
	
	public static String getValueByKey1(String result, String key) {
		JSONArray value = null;
		if (!isEmpty(result)) {
			try {
				JSONObject jsonObject = new JSONObject(result);
			    value = jsonObject.getJSONArray(key);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		try {
			return value.getString(0);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static boolean checkHpsTimeOut(String result) {
		boolean flag = false;
		if (!isEmpty(result)) {
			try {
				JSONObject jsonObject = new JSONObject(result);
				String resultCode = jsonObject.getString("Result");
				if (resultCode != null && "-10242504".equals(resultCode)) {
					flag = true;
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}
	
	public static boolean checkCallBackStat(int code, String sessionId) {
		boolean stat = false;
		if (code == 0 && !isEmpty(sessionId)) {
			stat = true;
		}
		return stat;
	}
	
    public static String getMobileNum(Context ctx) {
        try {
            TelephonyManager mTelephonyMgr = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
            String tel = mTelephonyMgr.getLine1Number();
            if (tel == null) {
                tel = "";
            }
            return tel;
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return null;
    }
}
